package com.cb.spzx.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.spzx.manager.service.BrandService;
import com.cb.spzx.model.entity.product.Brand;
import com.cb.spzx.model.vo.common.Result;
import com.cb.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "品牌管理")
@RestController
@RequestMapping(value="/admin/product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Operation(summary = "查询品牌列表")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Integer page, @PathVariable Integer limit){
        Page<Brand> brandPage = new Page<>(page, limit);
        brandService.findByPage(brandPage);
        return Result.build(brandPage, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加品牌")
    @PostMapping("save")
    public Result saveBrand(@RequestBody Brand brand){
        int result=brandService.saveBrand(brand);
        if(result>0){
            return Result.build(null,ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @Operation(summary = "修改品牌")
    @PutMapping("updateById")
    public Result updateBrandById(@RequestBody Brand brand) {
        brandService.updateBrandById(brand);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        brandService.deleteById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "查询所有品牌")
    @GetMapping("/findAll")
    public Result findAll() {
        List<Brand> list = brandService.findAll();
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}
